import request from "../../utils/request";
import {
  areaList
} from '@vant/area-data';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    list: [],
    total: 0,
    showArea: false,
    area: [],
    name: "",
    phone: "",
    shipping_addres: "",
    addressShow: false,
    type: "",
    isDefault: 0
  },
  isDefaultChange(e) {
    console.log(e);
    this.setData({
      isDefault: e.detail
    })
  },
  addBtn() {
    this.setData({
      addressShow: !this.data.addressShow
    })
  },
  selectArea() {
    this.setData({
      showArea: !this.data.showArea
    })
  },
  confirmArea(e) {
    console.log(e.detail.values);
    this.setData({
      area: e.detail.values,
      showArea: false
    })
  },
  async confirmAddress() {
    wx.showLoading({
      mask: true
    });
    const area = this.data.area.map(i => i.name).join("");
    const res = await request.post('/address', {
      name: this.data.name,
      phone: this.data.phone,
      address: area,
      detailAddress: this.data.shipping_addres,
      userId: wx.getStorageSync('userInfo').id,
      isDefault: this.data.isDefault,
    });
    if (res.data) {
      wx.hideLoading();
      wx.showToast({
        title: "添加收货地址成功",
        icon: "none"
      })
      this.setData({
        addressShow: false
      })
      this.getList();
    }
    console.log(res);
  },
  async getList() {
    const userId = wx.getStorageSync('userInfo').id;
    const res = await request.get(`/address/page?userId=${userId}`);
    console.log(res);
    this.setData({
      list: res.data.data
    })
  },
  delAddress(e) {
    console.log(e);
  },
  select(e) {
    const {
      item
    } = e.currentTarget.dataset;
    if (this.data.type === 'select') {
      console.log("选择地址");
      wx.setStorageSync('selectAddress', item);
      wx.navigateBack();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type || ''
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})